package com.sanjin.controller;

import com.sanjin.annotations.SysLogOperation;
import com.sanjin.convert.ParkLotConvert;
import com.sanjin.convert.ParkSpaceConvert;
import com.sanjin.entity.ParkLot;
import com.sanjin.entity.ParkSpace;
import com.sanjin.exception.MyException;
import com.sanjin.form.ParkLotForm;
import com.sanjin.form.ParkSpaceForm;
import com.sanjin.form.interfaces.Create;
import com.sanjin.form.interfaces.Update;
import com.sanjin.service.ParkLotService;
import com.sanjin.service.ParkSpaceService;
import com.sanjin.utils.ConvertUtils;
import com.sanjin.utils.ResponseVoUtils;
import com.sanjin.vo.PageVo;
import com.sanjin.vo.ResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description:
 * @author: sanjin
 * @date: 2019/5/5 23:37
 */
@Api(tags = "停车位接口")
@RestController
@RequestMapping("/parkSpace")
public class ParkSpaceController {
    @Autowired
    private ParkSpaceService parkSpaceService;

    @Autowired
    private ParkSpaceConvert parkSpaceConvert;

    @Autowired
    private ConvertUtils convertUtils;


    @SysLogOperation(value = "【创建停车位】")
    @ApiOperation("创建停车位")
    @PostMapping("")
    public ResponseVo create(@RequestBody @Validated(Create.class) ParkSpaceForm parkSpaceForm) {
        ParkSpace parkSpace = parkSpaceConvert.parkSpaceForm2ParkSpace(parkSpaceForm);
        parkSpaceService.createParkSpace(parkSpace);
        return ResponseVoUtils.success(parkSpace);
    }

    @SysLogOperation(value = "【删除停车位】")
    @ApiOperation("删除停车位")
    @DeleteMapping("")
    public ResponseVo delete(@RequestBody List<Integer> ids) {
        if (ids == null) throw new MyException("ids must not be null");
        parkSpaceService.deleteParkSpaceByParkSpaceIds(ids);
        return ResponseVoUtils.success();
    }

    @SysLogOperation(value = "【修改停车位】")
    @ApiOperation("修改停车位")
    @PutMapping("")
    public ResponseVo update(@RequestBody @Validated(Update.class) ParkSpaceForm parkSpaceForm) {
        ParkSpace parkSpace = parkSpaceConvert.parkSpaceForm2ParkSpace(parkSpaceForm);
        parkSpaceService.updateParkSpace(parkSpace);
        return ResponseVoUtils.success(parkSpace);
    }

    @ApiOperation("Page显示停车场")
    @GetMapping("/list")
    public ResponseVo list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                           @RequestParam(value = "size", defaultValue = "10") Integer size) {
        PageRequest pageRequest = PageRequest.of(page-1,size);
        Page<ParkSpace> parkSpacePage = parkSpaceService.queryParkSpaceByPage(pageRequest);
        PageVo<ParkSpace> pagevo = convertUtils.page2PageVo(parkSpacePage);
        return ResponseVoUtils.success(pagevo);
    }
}
